App.require(function () {
    var app = App.getCurrent();

    app.definePage("strdatePickerSecond", {
        getDomObj: function () {
            this.attachDom('[role="formitem"]', "textField")
                .attachDom("input", "input")
                .attachEvent("input", "keydown", this.keyDownHandler, false)
                .attachEvent("input", "input", this.inputHandler, false);
        },
        keyDownHandler: function (ev) {
            if (!/\d/.test(ev.key) && ev.key != "Backspace" && ev.key != "ArrowLeft" && ev.key != "ArrowRight") ev.preventDefault();
            var value = ev.target.value;
            if (value.length == 9 && parseInt(value.slice(8)) == 3) {
                if (parseInt(ev.key) > 1) ev.preventDefault();
            }
        },
        inputHandler: function (ev) {
            var target = ev.target, value = target.value;
            value = ("" + value).replace(/-/g, "");
            if (value.length > 4) value = value.slice(0, 4) + "-" + value.slice(4);
            if (value.length > 7) value = value.slice(0, 7) + "-" + value.slice(7);
            if (value.length == 6) {
                var num = parseInt(value.slice(5));
                if (num > 1) value = value.slice(0, 5) + "0" + num + "-";
            }
            if (value.length == 7) {
                var num = parseInt(value.slice(5)), tNum = parseInt(num / 10), zNum = num % 10;
                if (num > 12) value = value.slice(0, 5) + "0" + tNum + "-" + zNum;
            }
            if (value.length == 9) {
                var num = parseInt(value.slice(8));
                if (num > 3) value = value.slice(0, 8) + "0" + num;
            }
            target.value = value;

            if (value.length == 10) {
                this._computedDate(parseInt(value.slice(0, 4)), parseInt(value.slice(0, 4)), parseInt(value.slice(8, 10)));
            }
        },
        _computedDate: function (year, month, day) {
            var data = this.data, yDate = data.yDate, minDate = data.minDate, maxDate = data.maxDate;
            yDate.year = year;
            yDate.month = month;
            yDate.day = day;
            if (maxDate && maxDate.compare(year, month, day) === -1) {
                yDate.year = maxDate.year;
                yDate.month = maxDate.month;
                yDate.day = maxDate.day;
            }
            if (minDate && minDate.compare(year, month, day) === 1) {
                yDate.year = minDate.year;
                yDate.month = minDate.month;
                yDate.day = minDate.day;
            }
        },
        render: function (next) {
            var parentData = this.parent.data;
            this.data.yDate = parentData.yDate;
            this.data.minDate = parentData.minDate;
            this.data.maxDate = parentData.maxDate;
            this.fetch("./index.html", function (html) {
                next(html);
            })
        },
        init: function () {
            var input = this.domList.input, textField = this.domList.textField;
            textField.classList.add("str-text-edit");
            textField.classList.add("str-text-color");
            input.focus();
            input.value = this.data.yDate.toFormatString();
        },
    })
});